package com.szml.controller;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.szml.entity.User;
import com.szml.service.UserService;
import com.szml.utils.JWTUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public Map<String,Object> login(User user){
        log.info("用户名：{}",user.getName());
        log.info("密码：{}",user.getPassword());
        Map<String,Object> map = new HashMap<>();
//        User user = User.builder()
//                .name(name)
//                .password(password)
//                .build();
        try{
            User userDB = userService.login(user);
            Map<String,String> payload = new HashMap<>();
            payload.put("id", userDB.getId());
            payload.put("name", user.getName());
            //生成JWT的令牌
            String token = JWTUtils.getToken(payload);
            map.put("state", true);
            map.put("msg", "登录成功");
            map.put("token", token);//响应token
        }catch (Exception e){
            map.put("state", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

//    //受保护的接口，可以通过token进行校验
//    @PostMapping("/test")
//    public Map<String,Object> test(String token){
//        HashMap<String, Object> map = new HashMap<>();
//        log.info("当前的token为：{}",token);
//        try {
//            DecodedJWT verify = JWTUtils.verify(token); //验证令牌
//            //处理业务逻辑
//            //成功
//            map.put("status",true);
//            map.put("msg", "请求成功");
//            return map;
//        } catch (SignatureVerificationException e) {
//            //签名异常
//            e.printStackTrace();
//            map.put("msg","无效签名");
//        } catch (TokenExpiredException e){
//            //token过期异常
//            e.printStackTrace();
//            map.put("msg","token过期");
//        } catch (AlgorithmMismatchException e){
//            //算法不匹配异常
//            e.printStackTrace();
//            map.put("msg","算法不一致");
//        } catch (Exception e){
//            e.printStackTrace();
//            map.put("msg","token无效");
//        }
//        map.put("status", false);
//        return map;
//    }

    //受保护的接口，可以通过token进行校验 (这里已经通过拦截器拦截了，因此不用再传token了)
    @PostMapping("/test")
    public Map<String,Object> test(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        //处理自己的业务逻辑
        //...
        String token = request.getHeader("token");
        System.out.println(token);
        DecodedJWT verify = JWTUtils.verify(token);
        log.info("用户id:{}", verify.getClaim("id").asString());
        log.info("用户name:{}", verify.getClaim("name").asString());
        map.put("status",true);
        map.put("msg", "请求成功");
        return map;
    }
}
